package cn.stylefeng.guns.modular.stock.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.stock.entity.StockStrategy;
import cn.stylefeng.guns.modular.stock.model.StockBalance;
import cn.stylefeng.guns.modular.stock.model.StockPosition;
import cn.stylefeng.guns.modular.stock.model.params.StockStrategyParam;
import cn.stylefeng.guns.modular.stock.service.StockStrategyService;
import cn.stylefeng.guns.unitl.UnicodeConverterUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;
import java.util.Objects;


/**
 * 控制器
 *
 * @author xzan
 * @Date 2020-03-15 22:16:13
 */
@Controller
@RequestMapping("/stock/simulate")
public class SimulateTradeController extends BaseController {

    private String PREFIX = "/stock/stockStrategy";
    private final static String sinaStockUrl = "http://hq.sinajs.cn/list=";
    private final static String TRADE_CLIENT_ADDR = "http://abc.lemego.xyz:17155";

    @Autowired
    private StockStrategyService stockStrategyService;

    /**
     * 跳转到主页面
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/stockStrategy_app.html";
    }

    /**
     * 新增页面
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/stockStrategy_add.html";
    }

    /**
     * 编辑页面
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/stockStrategy_edit.html";
    }

    /**
     * 新增接口
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StockStrategyParam stockStrategyParam) {
        this.stockStrategyService.add(stockStrategyParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StockStrategyParam stockStrategyParam) {
        this.stockStrategyService.update(stockStrategyParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StockStrategyParam stockStrategyParam) {
        this.stockStrategyService.delete(stockStrategyParam);
        return ResponseData.success();
    }

    @RequestMapping("/sellDirectly")
    @ResponseBody
    public ResponseData sellDirectly(StockStrategyParam stockStrategyParam) {
        this.stockStrategyService.sellDirectly(stockStrategyParam);
        return ResponseData.success();
    }

    @RequestMapping("/buyDirectly")
    @ResponseBody
    public ResponseData buyDirectly(StockStrategyParam stockStrategyParam) {
        this.stockStrategyService.buyDirectly(stockStrategyParam);
        return ResponseData.success();
    }

    @RequestMapping("/monitor/page")
    public String monitor(StockStrategyParam stockStrategyParam) {
        return PREFIX + "/monitor.html";
    }


    /**
     * 查看详情接口
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StockStrategyParam stockStrategyParam) {
        StockStrategy detail = this.stockStrategyService.getById(stockStrategyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author xzan
     * @Date 2020-03-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StockStrategyParam stockStrategyParam) {
        return this.stockStrategyService.findPageBySpec(stockStrategyParam);
    }

    @ResponseBody
    @RequestMapping("/sina")
    public ResponseData sinaStockInfo(StockStrategyParam stockStrategyParam) {
        if (StringUtils.isBlank(stockStrategyParam.getCode())) {
            return ResponseData.success();
        }
        ResponseData stockCode = getStockCodeData(stockStrategyParam);
        if (stockCode != null) return stockCode;
        return ResponseData.success();

    }

    private ResponseData getStockCodeData(StockStrategyParam stockStrategyParam) {
        String content = HttpUtil.get(sinaStockUrl + stockStrategyParam.getCode());
        if (StringUtils.isNotBlank(content)) {
            String[] splitStrings = content.split("=");
            if (!Objects.isNull(splitStrings) && splitStrings.length == 2) {
                String stockInfo = splitStrings[1];
                stockInfo = stockInfo.replace("\"", "");
                String[] stockCompleteInfo = stockInfo.split(",");
                String stockCode = stockCompleteInfo[0];
                return ResponseData.success(stockCode);
            }
        }
        return null;
    }

    @RequestMapping("/reloadTradeClient")
    @ResponseBody
    public ResponseData reloadTradeClient() {
        HttpRequest.get(TRADE_CLIENT_ADDR + "/prepare").timeout(10000).executeAsync();
        return ResponseData.success();
    }

    @RequestMapping("/getBalance")
    @ResponseBody
    public ResponseData getAccount() {
        return ResponseData.success(getStockBalance());
    }

    @RequestMapping("/balance/page")
    public String balance() {
        return PREFIX + "/stockBalance_edit.html";
    }

    @RequestMapping("/position/page")
    public String position() {
        return PREFIX + "/stockPosition_app.html";
    }

    @RequestMapping("/getPosition")
    @ResponseBody
    public ResponseData getPosition() {
        return ResponseData.success(getStockPosition());
    }

    public static void main(String[] args) {
//        String sinaStockUrl = "http://backtest.10jqka.com.cn/tradebacktest/historypick?query=%E9%95%BF%E4%B8%8A%E5%BD%B1%E7%BA%BF%E5%B0%8F%E4%BA%8E%E5%AE%9E%E4%BD%93%E7%9A%840.6%E5%80%8D%EF%BC%9B%E4%B8%8B%E5%BD%B1%E7%BA%BF%E5%B0%8F%E4%BA%8E%E5%AE%9E%E4%BD%93%E7%9A%843.5%E5%80%8D%EF%BC%9B%E6%8C%AF%E5%B9%85%E5%B0%8F%E4%BA%8E15%25%EF%BC%9B5%E6%97%A5%E5%9D%87%E7%BA%BF%E5%A4%A7%E4%BA%8E+10+%E6%97%A5%E5%9D%87%E7%BA%BF%3B5+%E6%97%A5%E5%9D%87%E7%BA%BF%E8%A7%92%E5%BA%A6%E5%A4%A7%E4%BA%8E+75.43%E5%B0%8F%E4%BA%8E+81.80%3B%E6%8D%A2%E6%89%8B%E7%8E%87%E5%B0%8F%E4%BA%8E19%25%3B%E9%9D%9E%E5%88%9B%E4%B8%9A%E6%9D%BF%EF%BC%9B%E6%B6%A8%E5%B9%85%E5%A4%A7%E4%BA%8E-9%25%E5%B0%8F%E4%BA%8E0%3B%E6%9C%80%E4%BD%8E%E8%82%A1%E4%BB%B7%E5%A4%A7%E4%BA%8E+10+%E6%97%A5%E5%9D%87%E7%BA%BF%3B%E5%B8%82%E7%9B%88%E7%8E%87%E5%B0%8F%E4%BA%8E125%E5%80%8D,%E6%94%B6%E7%9B%98%E4%BB%B7%E5%B0%8F%E4%BA%8E%E5%BC%80%E7%9B%98%E4%BB%B7%3B%E9%9D%9E%E5%88%9B%E4%B8%9A%E6%9D%BF%3Bkdj%E5%B0%8F%E4%BA%8E90%EF%BC%9B%E4%B8%AD%E5%B0%8F%E7%9B%98%EF%BC%9BWR%E5%A4%A7%E4%BA%8E10%E5%B0%8F%E4%BA%8E80&hold_num=1&trade_date=2020-07-02";
//        System.out.println(HttpUtil.get(sinaStockUrl));
        //getStockPosition();
        getStockBalance();

    }

    /**
     * get stock position
     *
     * @return
     */
    private static List<StockPosition> getStockPosition() {
        String content = HttpUtil.get(TRADE_CLIENT_ADDR + "/position", 10000);
        if (StringUtils.isBlank(content)) {
            return Collections.EMPTY_LIST;
        }
        content = UnicodeConverterUtil.unicode2String(content);
        JSONArray jsonArray = JSONUtil.parseArray(content);
        if (jsonArray.size() == 0) {
            return Collections.EMPTY_LIST;
        }
        List<StockPosition> stockPositions = JSONUtil.toList(jsonArray, StockPosition.class);
        if (CollectionUtil.isEmpty(stockPositions)) {
            return Collections.EMPTY_LIST;
        }
        return stockPositions;
    }

    /**
     * get stock balance
     *
     * @return
     */
    private static StockBalance getStockBalance() {
        String content = HttpUtil.get(TRADE_CLIENT_ADDR + "/balance", 10000);
        if (StringUtils.isBlank(content)) {
            return new StockBalance();
        }
        content = UnicodeConverterUtil.unicode2String(content);
        JSONObject jsonObject = JSONUtil.parseObj(content);
        StockBalance stockBalance = JSONUtil.toBean(jsonObject, StockBalance.class);
        return stockBalance;
    }

}


